<?php

/**
 * 
 */
class Storage_AwsS3 extends Storage_Abstract {

    /**
     * 根链接
     * @var <type>
     */
    private $_baseUrl = '';

    /**
     * 桶名称
     * @var type 
     */
    private $_bucket = '';

    /**
     * 是否已经初始化
     * @var type 
     */
    private static $_inited = false;

    /**
     * 构造函数
     * @param array $option
     */
    public function __construct(array $option = array()) {

        $this->_baseUrl = $option['base_url'];
        $this->_bucket = $option['bucket'];
        if (!$this->_bucket) {
            throw new Storage_Exception('bucket can not bu empty', Storage_Exception::E_STORAGE_INIT_ERROR);
        }
        parent::__construct($option);

        // init
        self::_init();
    }

    /**
     * 获取 client
     * @return type
     */
    private static function _init() {

        if (self::$_inited) {
            return true;
        }

        self::$_inited = true;
        require_once dirname(dirname(__FILE__)) . '/aws/aws-autoloader.php';
        $conf = V::config('aws_client', []) + [
            'profile' => 'default',
            'credentials_filename' => CONFIG_PATH . '/aws.credentials',
            'region' => 'ap-southeast-1',
            'version' => 'latest'
        ];
        $client = new Aws\S3\S3Client($conf);
        $client->registerStreamWrapper();
    }

    /**
     * 判断是否 腾讯云cos （兼容）
     * @return type
     */
    private static function _isQcloud() {
        
        return Str::has(V::config('aws_client.endpoint', ''), 'qcloud') ? true : false;
    }

    /**
     * 获取 URL 
     * @param type $path
     * @return type
     */
    public function getUrl($path) {

        if (self::_isQcloud()) {
            return $this->_baseUrl . '/' . ltrim($path, '\\/');
        }
        return $this->_baseUrl . '/' . $this->_bucket . '/' . ltrim($path, '\\/');
    }

    /**
     * 获取路径
     * @param type $path
     */
    private function _getPath($path) {

        return 's3://' . $this->_bucket . '/' . ltrim($path, '\\/');
    }

    /**
     * 检测文件是否存在
     * @param type $path
     * @return type
     */
    public function isExist($path) {

        return file_exists($this->_getPath($path));
    }

    /**
     * 读取指定文件内容
     * @param type $path
     * @return type
     */
    public function read($path) {

        return file_get_contents($this->_getPath($path));
    }

    /**
     * 删除指定文件
     * @param type $path
     * @return type
     */
    public function remove($path) {

        return unlink($this->_getPath($path));
    }

    /**
     * 删除指定目录
     * @param type $path
     * @return type
     */
    public function rmdir($path) {

        return rmdir($this->_getPath($path));
    }

    /**
     * 上传文件
     * @param type $path
     * @param type $localPath
     * @return type
     */
    public function upload($path, $localPath) {

        return $this->write($path, file_get_contents($localPath));
    }

    /**
     * 写入内容到指定文件
     * @param type $path
     * @param type $content
     * @return type
     */
    public function write($path, $content) {

        return file_put_contents($this->_getPath($path), $content);
    }

}
